/**
 * Created by yuweimei on 2018/3/28.
 */
app.controller('other_home', function($scope, $http) {
	//显示删除操作页面
	$scope.allShow = function(tourId) {
		$scope.tourId = tourId;
		$('.all').css("display", "block");
	};
	//隐藏删除操作页面
	$scope.allHide = function() {
		$('.all').css("display", "none");
	};

	$('.alert_top').click(function() {
		$('.all').css("display", "none");
	});

	//证书
	var token = localStorage.getItem('token');
	var OtherUserId = sessionStorage.getItem('OtherUserId');
	//Other用户信息对象
	$scope.showCircle = false;
	$scope.userInfo = {};
	//查询登录用户点赞动态列表
	$scope.tourIdLikeList = [];
    //当前用户收藏动态id
    $scope.tourIdCollList = [];
    //用户收藏记录
    $scope.showCollectionList = function () {
        if (token) {
            $http({
                method: 'get',
                url: app.netPath + '/collection/show',
                headers: {'Authorization': token}
            }).then(function (res) {
                if (!res.data.error) {
                    for (var i = 0; i < res.data.data.length; i++) {
                        $scope.tourIdCollList.push(res.data.data[i].collDynaid);
                    }
                }
            });
        }
    };
	$scope.showLikeList = function() {
        $scope.showCollectionList();
		if(!token) {
			$scope.getPerInfo();
		} else {
			//点赞动态列表id
			$http({
				method: 'get',
				url: app.netPath + '/dynamic/details/show/likes',
				headers: {
					'Authorization': token
				}
			}).then(function(res) {
				if(!res.data.error) {
					for(var i = 0; i < res.data.data.length; i++) {
						$scope.tourIdLikeList.push(res.data.data[i].likedynaId);
					}
					$scope.getPerInfo();
				}
			});
		}
	};
	// 个人信息列表
	$scope.getPerInfo = function() {
		$http({
			method: 'get',
			url: app.netPath + '/dynamic/showTour',
			headers: {
				'Authorization': token
			},
			params: {
				userId: OtherUserId
			}
		}).then(function(res) {
			if(!res.data.error) {
				$scope.userInfo = res.data.data;
				console.log(res.data.data);

				var avatarImg = {};

				if(res.data.data.userAvatarAddress) {
					avatarImg.imgAddress = app.netPath + '/resources/img/' + res.data.data.userAvatarAddress;
					$scope.userInfo.avatarImg = avatarImg;
				}

				if(res.data.data.userDetail) {
					$scope.userDetail = $scope.userInfo.userDetail;
				}

				$scope.userInfo.userSexWord = $scope.userInfo.userSex == 0 ? '男' : $scope.userInfo.userSex == 1 ? '女' : '点击选择';
				var index = $(".sex").text();
				if($scope.userInfo.userSex == 0) {
					$(".sex_boy").css("background", "blue");
					$(".sex_icon").addClass("fa-mars")
				} else {
					$(".sex_boy").css("background", "#fe8faf");
					$(".sex_icon").addClass("fa-venus")
				}

				var thisTime = new Date();
				$scope.userInfo.userAge = parseInt((parseInt(thisTime.valueOf()) - parseInt($scope.userInfo.userBirthday).valueOf()) / (365 * 24 * 60 * 60 * 1000));
				if($scope.userInfo.userNickName) {
					$scope.words = $scope.userInfo.userNickName;
				} else {
					$scope.words = "不一样的昵称，不一样的心情";
				}
				for(var i = 0; i < res.data.data.dynamicsList.length; i++) {
					//放置动态图片
					if(res.data.data.dynamicsList[i].tourdynaImg) {
						//得到每条动态的图片数组
						var dynamicImgs = app.getDynamicImg(res.data.data.dynamicsList[i].tourdynaImg);
						for(var j = 0; j < dynamicImgs.length; j++) {
							dynamicImgs[j] = app.netPath + '/resources/taskImg/' + dynamicImgs[j];
							app.buildImageUrl(dynamicImgs, j, function() {
								$scope.$digest();
							});
						}
						res.data.data.dynamicsList[i].tourdynaImg = dynamicImgs;
					}
					for(var a = 0; a < $scope.tourIdLikeList.length; a++) {
						if(res.data.data.dynamicsList[i].tourdynaId == $scope.tourIdLikeList[a]) {
							res.data.data.dynamicsList[i].isLike = 1;
						} else {

						}
					}
                    for (var a = 0; a < $scope.tourIdCollList.length; a++) {
                        if(res.data.data.dynamicsList[i].tourdynaId ==  $scope.tourIdCollList[a]) {
                            res.data.data.dynamicsList[i].isColl = 1;
                        } else {

                        }
                    }
                    console.log(res.data.data[i]);
					var lat = res.data.data.dynamicsList[i].positionX;
					var lng = res.data.data.dynamicsList[i].positionY;
					res.data.data.dynamicsList[i].distance = app.getGreatCircleDistance(lat, lng);
					if(res.data.data.dynamicsList[i].distance >= 1000) {
						res.data.data.dynamicsList[i].distance = (app.getGreatCircleDistance(lat, lng) / 1000).toFixed(2) + "千米";
					} else {
						res.data.data.dynamicsList[i].distance = app.getGreatCircleDistance(lat, lng) + "米";
					}
				}
				console.log(res.data.data);
			} else {
				$.toast(res.data.error + ',' + res.data.msg);
			}
		}, function() {
			$.toast('网络走丢了！')
		});
	};
	//添加收藏
	$scope.addCollection = function(tourId,index) {
		if(!token) {
			//判断有无证书
			$.toast("请先登录");
			location.href = '#login';
			return;
		} else {
			$.confirm('确定收藏？', '', function() {
				//欲删除的动态id
				// var tourId = $scope.tourId;
				$http({
					method: 'get',
					url: app.netPath + '/collection/add',
					headers: {
						'Authorization': token
					},
					params: {
						tourdynaId: tourId
					}
				}).then(function(res) {
					if(!res.data.error) {
						$('.all').css("display", "none");
						$.toast('收藏成功');
                        $scope.userInfo.dynamicsList[index].isColl = 1;
					} else {
						if(104 == res.data.error) {
							$('.all').css("display", "none");
							$.toast("已收藏，请前往我的收藏查看");
						} else {
							$.toast(res.data.error + ',' + res.data.msg);
						}
					}
				}, function() {
					$.toast('网络走丢了！')
				});
			}, function() {
				$('.all').css("display", "none");
				$.toast('已取消');
			});
		}

	};

	//查询当前登录用户关注的用户列表
	$scope.showFollows = function() {
		if(!token) {
			$scope.isFollow = false;
		} else {
			$http({
				method: 'get',
				url: app.netPath + '/follow/booleanFollows',
				headers: {
					'Authorization': token
				},
				params: {
					userId: OtherUserId
				}
			}).then(function(res) {
				if(!res.data.error) {
					$scope.isFollow = false;
				} else {
					$scope.isFollow = true;
				}
			}, function() {
				$.toast('网络走丢了！')
			});
		}

	};
	//关注该用户
	$scope.addFollow = function(userId) {
		if(!token) {
			//判断有无证书
			$.toast("请先登录");
			location.href = '#login';
			return;
		} else {
			$http({
				method: 'get',
				url: app.netPath + '/follow/add',
				headers: {
					'Authorization': token
				},
				params: {
					UserId: userId
				}
			}).then(function(res) {
				if(!res.data.error) {
					$scope.isFollow = true;
					$scope.showFollows();
					$.toast('关注成功');
                    $scope.userInfo.fans=$scope.userInfo.fans+1;
				} else {
					$.toast(res.data.msg);
					$scope.isFollow = false;
					$scope.showFollows();
				}
			}, function() {
				$.toast('网络走丢了！')
			});
		}

	};
	//取消关注
	$scope.reduceFollow = function(userId) {
		if(!token) {
			//判断有无证书
			$.toast("请先登录");
			location.href = '#login';
			return;
		} else {
			$http({
				method: 'get',
				url: app.netPath + '/follow/delete',
				headers: {
					'Authorization': token
				},
				params: {
					userTId: userId
				}
			}).then(function(res) {
				if(!res.data.error) {
					// $.toast('取关成功');
					$scope.isFollow = false;
                    $scope.userInfo.fans=$scope.userInfo.fans-1;
					$scope.showFollows();
				} else {
					$.toast(res.data.msg);
					$scope.isFollow = true;
					$scope.showFollows();
				}
			}, function() {
				$.toast('网络走丢了！')
			});
		}
	};

	//取消点赞
	$scope.reduceLike = function(tourdynaId, index) {
		if(!token) {
			//判断有无证书
			$.toast("请先登录");
			location.href = '#login';
			return;
		} else {
			$http({
				method: 'get',
				url: app.netPath + '/dynamic/details/likeReduce',
				headers: {
					'Authorization': token
				},
				params: {
					tourdynaId: tourdynaId
				}
			}).then(function(res) {
				if(!res.data.error) {
					// $.toast('已取消点赞');
					$scope.userInfo.dynamicsList[index].isLike = 0;
					$scope.userInfo.dynamicsList[index].tourdynaLikeNum = $scope.userInfo.dynamicsList[index].tourdynaLikeNum - 1;
				} else {
					// $.toast(res.data.error + ',' + res.data.msg);
				}
			}, function() {
				$.toast('网络走丢了！')
			});
		}
	};
	//点赞
	$scope.addLike = function(tourdynaId, index) {
		if(!token) {
			//判断有无证书
			$.toast("请先登录");
			location.href = '#login';
			return;
		} else {
			$http({
				method: 'get',
				url: app.netPath + '/dynamic/details/likeAdd',
				headers: {
					'Authorization': token
				},
				params: {
					tourdynaId: tourdynaId
				}
			}).then(function(res) {
				if(!res.data.error) {
					$scope.userInfo.dynamicsList[index].isLike = 1;
					$scope.userInfo.dynamicsList[index].tourdynaLikeNum = $scope.userInfo.dynamicsList[index].tourdynaLikeNum + 1;
				} else {
					// $.toast(res.data.error + ',' + res.data.msg);

				}
			}, function() {
				$.toast('网络走丢了！')
			});
		}
	};

	//跳转粉丝页面
	$scope.GoToLiker = function(userId) {
		//登录用户Id
		sessionStorage.setItem('MyUserId', userId);
	};
	//跳转关注页面
	$scope.GoToFollow = function(userId) {
		//登录用户Id
		sessionStorage.setItem('MyUserId', userId);
	};
	$scope.GoToReview = function(tourdynaId) {
		sessionStorage.setItem('tourdynaInfoId', tourdynaId);
	};

});